<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>迷路的机器人</title>
</head>

<body>
  <script>
    var pathWithObstacles = function (obstacleGrid) {
      let res = []
      let len1 = obstacleGrid.length, len2 = obstacleGrid[0].length
      function find(i, j, item) {
        if (i >= len1 || j >= len2) {  //设置递归条件
          if (i == len1 && j == len2 - 1) {  //说明有路径到达终点，获取结果
            res = item.concat();
          }
          return;
        }
        if (obstacleGrid[i][j] == 0) { //判断此位置是否能够通过
          obstacleGrid[i][j] = -1 //标识访问过的位置，只要不为0都行
          item.push([i, j]);  //记录路径
          find(i + 1, j, item);   //向下访问
          find(i, j + 1, item);    //向右访问
          item.pop();      //回溯
        }

      }
      find(0, 0, []);
      return res;

    };
    console.log(pathWithObstacles([[0, 0, 0], [0, 1, 0], [0, 0, 0]]))
  </script>
</body>

</html>